/*     */ package com.zimbra.cs.account.auth;
/*     */ 
/*     */ import com.zimbra.cs.ldap.unboundid.InMemoryLdapServer;
/*     */ import java.io.PrintStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.security.SecureRandom;
/*     */ import org.apache.commons.codec.binary.Base64;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class PasswordUtil
/*     */ {
/*     */   public static class SSHA
/*     */   {
/*  35 */     private static int SALT_LEN = 4;
/*  36 */     private static String SSHA_ENCODING = "{SSHA}";
/*     */     
/*     */     public static boolean isSSHA(String encodedPassword) {
/*  39 */       return encodedPassword.startsWith(SSHA_ENCODING);
/*     */     }
/*     */     
/*     */     public static boolean verifySSHA(String encodedPassword, String password) {
/*  43 */       if (!encodedPassword.startsWith(SSHA_ENCODING))
/*  44 */         return false;
/*  45 */       byte[] encodedBuff = encodedPassword.substring(SSHA_ENCODING.length()).getBytes();
/*  46 */       byte[] buff = Base64.decodeBase64(encodedBuff);
/*  47 */       if (buff.length <= SALT_LEN)
/*  48 */         return false;
/*  49 */       int slen = buff.length == 28 ? 8 : SALT_LEN;
/*  50 */       byte[] salt = new byte[slen];
/*  51 */       System.arraycopy(buff, buff.length - slen, salt, 0, slen);
/*  52 */       String generated = generateSSHA(password, salt);
/*  53 */       return generated.equals(encodedPassword);
/*     */     }
/*     */     
/*     */     public static String generateSSHA(String password, byte[] salt) {
/*     */       try {
/*  58 */         MessageDigest md = MessageDigest.getInstance("SHA1");
/*  59 */         if (salt == null)
/*     */         {
/*  61 */           if (InMemoryLdapServer.isOn())
/*     */           {
/*  63 */             salt = new byte[] { Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE };
/*     */           } else {
/*  65 */             salt = new byte[SALT_LEN];
/*  66 */             SecureRandom sr = new SecureRandom();
/*  67 */             sr.nextBytes(salt);
/*     */           }
/*     */         }
/*  70 */         md.update(password.getBytes("UTF-8"));
/*  71 */         md.update(salt);
/*  72 */         byte[] digest = md.digest();
/*  73 */         byte[] buff = new byte[digest.length + salt.length];
/*  74 */         System.arraycopy(digest, 0, buff, 0, digest.length);
/*  75 */         System.arraycopy(salt, 0, buff, digest.length, salt.length);
/*  76 */         return SSHA_ENCODING + new String(Base64.encodeBase64(buff));
/*     */       }
/*     */       catch (NoSuchAlgorithmException e) {
/*  79 */         throw new RuntimeException(e);
/*     */       }
/*     */       catch (UnsupportedEncodingException e) {
/*  82 */         throw new RuntimeException(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static class SSHA512
/*     */   {
/*  93 */     private static int SALT_LEN = 8;
/*  94 */     private static String SSHA512_ENCODING = "{SSHA512}";
/*     */     
/*     */     public static boolean isSSHA512(String encodedPassword) {
/*  97 */       return encodedPassword.startsWith(SSHA512_ENCODING);
/*     */     }
/*     */     
/*     */     public static boolean verifySSHA512(String encodedPassword, String password) {
/* 101 */       if (!encodedPassword.startsWith(SSHA512_ENCODING))
/* 102 */         return false;
/* 103 */       byte[] encodedBuff = encodedPassword.substring(SSHA512_ENCODING.length()).getBytes();
/* 104 */       byte[] buff = Base64.decodeBase64(encodedBuff);
/* 105 */       if (buff.length <= SALT_LEN)
/* 106 */         return false;
/* 107 */       int slen = buff.length == 28 ? 8 : SALT_LEN;
/* 108 */       byte[] salt = new byte[slen];
/* 109 */       System.arraycopy(buff, buff.length - slen, salt, 0, slen);
/* 110 */       String generated = generateSSHA512(password, salt);
/* 111 */       return generated.equals(encodedPassword);
/*     */     }
/*     */     
/*     */     public static String generateSSHA512(String password, byte[] salt) {
/*     */       try {
/* 116 */         MessageDigest md = MessageDigest.getInstance("SHA-512");
/* 117 */         if (salt == null)
/*     */         {
/* 119 */           if (InMemoryLdapServer.isOn())
/*     */           {
/* 121 */             salt = new byte[] { Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE };
/*     */           } else {
/* 123 */             salt = new byte[SALT_LEN];
/* 124 */             SecureRandom sr = new SecureRandom();
/* 125 */             sr.nextBytes(salt);
/*     */           }
/*     */         }
/* 128 */         md.update(password.getBytes("UTF-8"));
/* 129 */         md.update(salt);
/* 130 */         byte[] digest = md.digest();
/* 131 */         byte[] buff = new byte[digest.length + salt.length];
/* 132 */         System.arraycopy(digest, 0, buff, 0, digest.length);
/* 133 */         System.arraycopy(salt, 0, buff, digest.length, salt.length);
/* 134 */         return SSHA512_ENCODING + new String(Base64.encodeBase64(buff));
/*     */       }
/*     */       catch (NoSuchAlgorithmException e) {
/* 137 */         throw new RuntimeException(e);
/*     */       }
/*     */       catch (UnsupportedEncodingException e) {
/* 140 */         throw new RuntimeException(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static class SHA1
/*     */   {
/* 151 */     private static String SHA1_ENCODING = "{SHA1}";
/* 152 */     private static String SHA_ENCODING = "{SHA}";
/*     */     
/*     */     public static boolean isSHA1(String encodedPassword) {
/* 155 */       return (encodedPassword.startsWith(SHA1_ENCODING)) || (encodedPassword.startsWith(SHA_ENCODING));
/*     */     }
/*     */     
/*     */ 
/*     */     public static boolean verifySHA1(String encodedPassword, String password)
/*     */     {
/* 161 */       String prefix = SHA1_ENCODING;
/* 162 */       if (encodedPassword.startsWith(SHA1_ENCODING)) {
/* 163 */         prefix = SHA1_ENCODING;
/* 164 */       } else if (encodedPassword.startsWith(SHA_ENCODING)) {
/* 165 */         prefix = SHA_ENCODING;
/*     */       } else
/* 167 */         return false;
/* 168 */       byte[] encodedBuff = encodedPassword.substring(prefix.length()).getBytes();
/* 169 */       byte[] buff = Base64.decodeBase64(encodedBuff);
/* 170 */       String generated = generateSHA1(password, prefix);
/* 171 */       return generated.equals(encodedPassword);
/*     */     }
/*     */     
/*     */     public static String generateSHA1(String password) {
/* 175 */       return generateSHA1(password, SHA1_ENCODING);
/*     */     }
/*     */     
/*     */     public static String generateSHA1(String password, String prefix) {
/* 179 */       if (prefix == null) prefix = SHA1_ENCODING;
/*     */       try {
/* 181 */         MessageDigest md = MessageDigest.getInstance("SHA1");
/* 182 */         md.update(password.getBytes("UTF-8"));
/*     */         
/* 184 */         byte[] digest = md.digest();
/* 185 */         return prefix + new String(Base64.encodeBase64(digest));
/*     */       }
/*     */       catch (NoSuchAlgorithmException e) {
/* 188 */         throw new RuntimeException(e);
/*     */       }
/*     */       catch (UnsupportedEncodingException e) {
/* 191 */         throw new RuntimeException(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static class MD5
/*     */   {
/* 201 */     private static String MD5_ENCODING = "{MD5}";
/*     */     
/*     */     public static boolean isMD5(String encodedPassword) {
/* 204 */       return encodedPassword.startsWith(MD5_ENCODING);
/*     */     }
/*     */     
/*     */     public static boolean verifyMD5(String encodedPassword, String password) {
/* 208 */       if (!encodedPassword.startsWith(MD5_ENCODING))
/* 209 */         return false;
/* 210 */       byte[] encodedBuff = encodedPassword.substring(MD5_ENCODING.length()).getBytes();
/* 211 */       byte[] buff = Base64.decodeBase64(encodedBuff);
/* 212 */       String generated = generateMD5(password);
/* 213 */       return generated.equals(encodedPassword);
/*     */     }
/*     */     
/*     */     public static String generateMD5(String password) {
/*     */       try {
/* 218 */         MessageDigest md = MessageDigest.getInstance("MD5");
/* 219 */         md.update(password.getBytes("UTF-8"));
/*     */         
/* 221 */         byte[] digest = md.digest();
/* 222 */         return MD5_ENCODING + new String(Base64.encodeBase64(digest));
/*     */       }
/*     */       catch (NoSuchAlgorithmException e) {
/* 225 */         throw new RuntimeException(e);
/*     */       }
/*     */       catch (UnsupportedEncodingException e) {
/* 228 */         throw new RuntimeException(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */   {
/* 235 */     String plain = "test123";
/* 236 */     System.out.println("plain:        " + plain);
/* 237 */     System.out.println("encoded SSHA: " + SSHA.generateSSHA(plain, null));
/* 238 */     System.out.println("encoded SSHA512: " + SSHA512.generateSSHA512(plain, null));
/* 239 */     System.out.println("encoded SSH1: " + SHA1.generateSHA1(plain));
/* 240 */     System.out.println("encoded MD5:  " + MD5.generateMD5(plain));
/* 241 */     System.out.println();
/*     */     
/* 243 */     plain = "helloWorld";
/* 244 */     System.out.println("plain:        " + plain);
/* 245 */     System.out.println("encoded SSHA: " + SSHA.generateSSHA(plain, null));
/* 246 */     System.out.println("encoded SSHA512: " + SSHA512.generateSSHA512(plain, null));
/* 247 */     System.out.println("encoded SSH1: " + SHA1.generateSHA1(plain));
/* 248 */     System.out.println("encoded MD5:  " + MD5.generateMD5(plain));
/* 249 */     System.out.println();
/*     */     
/* 251 */     plain = "testme";
/* 252 */     String encodedSHA1 = SHA1.generateSHA1(plain, SHA1.SHA1_ENCODING);
/* 253 */     String encodedSHA = SHA1.generateSHA1(plain, SHA1.SHA_ENCODING);
/*     */     
/* 255 */     boolean result = SHA1.verifySHA1(encodedSHA1, plain);
/* 256 */     System.out.println("result is " + (result ? "good" : "bad"));
/* 257 */     result = SHA1.verifySHA1(encodedSHA, plain);
/* 258 */     System.out.println("result is " + (result ? "good" : "bad"));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/auth/PasswordUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */